<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/4/4
 * Time: 10:45
 */

namespace app\admin\controller;
use PHPExcel;
use PHPExcel_IOFactory;
use app\common\model\Site;
use app\common\model\SiteTeam;
use app\admin\logic\RoleLogic;
use think\facade\Request;
use app\admin\logic\AdminLogic;
class Admin extends Base{

    private $adminLogic;
    private $roleLogic;

    public function __construct()
    {
        parent::__construct();
        $this->adminLogic = new AdminLogic();
        $this->roleLogic = new RoleLogic();
    }

    public function export(){
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
// 设置列的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
// 设置表头
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '序号');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', '管理员账户');
        $objPHPExcel->getActiveSheet()->SetCellValue('C1', '管理员角色');
        $objPHPExcel->getActiveSheet()->SetCellValue('D1', '所属站点');
        $objPHPExcel->getActiveSheet()->SetCellValue('E1', '所属服务队');
        $objPHPExcel->getActiveSheet()->SetCellValue('F1', '注册时间');

        $where = Request::param();
        if($where['type']=='all'){
            $where['limit'] = 100000;
        }
        $res = $this->adminLogic->ajaxIndex($where, $where['page']);
        $data = $res['data'];
        $num = 2;
        foreach ($data as $k => $v) {
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, ($k+1));
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $num, $v['name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $num, $v['role_name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $num, $v['site_name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $num, $v['team_name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $num, $v['create_time']);
            $num++;
        }
        $fileName = "管理员表" . date('Y-m-d', time()) . rand(1, 1000);
        $xlsName = iconv('utf-8', 'gb2312', $fileName);
        $objPHPExcel->getActiveSheet()->setTitle('sheet'); // 设置工作表名
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //下载 excel5与excel2007
        ob_end_clean(); // 清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
    }



    /**
     * 管理员首页
     * @return mixed
     */
    public function index(){
        return $this->fetch();
    }

    /**
     * 管理员异步列表
     * @return \think\response\Json
     */
    public function ajaxIndex(){
        $tempWhere = Request::param();
        $res = $this->adminLogic->ajaxIndex($tempWhere, self::$nowPage);
        return json($res);
    }

    /**
     * 添加/编辑管理员
     * @return mixed
     */
    public function add(){
        $id = Request::param('id');
        $act = 'add';
        $info = [];
        if ($id){
            $act = 'edit';
            $info = $this->adminLogic->info($id);
        }
        $roleList = $this->roleLogic->getAllRole();
        $this->assign('act', $act);
        $this->assign('info',$info);
        $this->assign('role_list', $roleList['data']);
        $this->assign('site',Site::select());
        $this->assign('team',SiteTeam::select());
        return $this->fetch();
    }
    //修改密码
    public function pwd(){
        if(Request::isPost()){
            $data = Request::param();
            $res = $this->adminLogic->pwd($data);
            return json($res);
        }else {
            $info = $_SESSION['xm_travel']['admin_info'];
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    /**
     * 处理管理员逻辑
     * @return \think\response\Json
     */
    public function handle(){
        $data = Request::param();
        $res = $this->adminLogic->handle($data);
        return json($res);
    }

}